import {getCategory,getSubcategory, getCategoryDetail} from "../../service/category"
Page({
  data: {
    categories: [],
    categoryData: {},
    currentIndex: 0,
    top:0
  },
  onLoad: function (options) {
    //请求分类数据
    this._getCategory()
  },
  _getCategory(){
    getCategory().then(e=>{
      //获取分类数据
      const categories = e.data.data.category.list
      const categoryData = {}
      for (let i = 0; i < categories.length; i++) {
        //给categoryData添加属性
        categoryData[i] = {
          subcategories: [],
          categoryDetail: []
        }
      }
      //修改数据
      this.setData({
        categories,
        categoryData
      })
      //请求第一个类别的数据
      this._getSubcategory(0)
      //请求第一个类别数据的详情数据
      this._getCategoryDetail(0)
    })
  },
  _getSubcategory(currentIndex){
    const maitKey = this.data.categories[currentIndex].maitKey;
    getSubcategory(maitKey).then(e=>{
      const tempCategoryData =this.data.categoryData
      tempCategoryData[currentIndex].subcategories = e.data.data.list
      this.setData({
        categoryData:tempCategoryData
      })
    })
  },
  _getCategoryDetail(currentIndex){
    const miniWallkey = this.data.categories[currentIndex].miniWallkey
    this._getRealCategoryDetail(currentIndex, miniWallkey, 'pop')
  },
  _getRealCategoryDetail(index,miniWallKey,type){
    getCategoryDetail(miniWallKey,type).then(e=>{
      console.log(e);
      //获取categoryData
      const categoryData = this.data.categoryData
      
      // 2.修改数据
      categoryData[index].categoryDetail = e;

      //修改setData数据
      this.setData({
        categoryData
      })
    })

  },
  menuClick(e){
    console.log(e);
    const currentIndex = e.detail.currentIndex;
    this.setData({
      currentIndex
    })
    //重新获取对应的数据
    this._getSubcategory(currentIndex)
    this._getCategoryDetail(currentIndex)
  },
  onPageScroll(e){
    const top = e.scrollTop
    this.setData({
      top
    })
  }
  
})